package com.pomelo.kill.service.impl;

import cn.hutool.core.lang.ObjectId;
import cn.hutool.system.UserInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.pomelo.kill.mapper.UserinfoMapper;
import com.pomelo.kill.model.Userinfo;
import com.pomelo.kill.service.UserinfoService;

@Service
public class UserinfoServiceImpl implements UserinfoService {

    @Resource
    private UserinfoMapper userinfoMapper;

    @Override
    public int deleteByPrimaryKey(Integer id, String openid) {
        return userinfoMapper.deleteByPrimaryKey(id, openid);
    }

    @Override
    public int insert(Userinfo record) {
        return userinfoMapper.insert(record);
    }

    @Override
    public int insertSelective(Userinfo record) {
        return userinfoMapper.insertSelective(record);
    }

    @Override
    public Userinfo selectByPrimaryKey(Integer id, String openid) {
        return userinfoMapper.selectByPrimaryKey(id, openid);
    }

    @Override
    public int updateByPrimaryKeySelective(Userinfo record) {
        return userinfoMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Userinfo record) {
        return userinfoMapper.updateByPrimaryKey(record);
    }

    @Override
    public Userinfo login(String openid) {
        Userinfo userinfo = new Userinfo();
        userinfo.setOpenid(openid);
        userinfo.setUsername(ObjectId.next());
        int i = insertSelective(userinfo);
        return i > 0 ? userinfo : null;
    }

    @Override
    public Userinfo selectByOpenid(String openid) {
        return userinfoMapper.selectByOpenid(openid);
    }

}
